package com.hsy.linemoney.apiservice;

import com.deparse.dpframe.tools.utils.DPGsonUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 点数相关
 * Created by MartinKent on 2016/10/25.
 */

public class PointService extends BasicService<JSONArray> {
//    获取我的利润和奖金
    public void getUserPointsReport(String reportDateString, final Callback<UserPointsReportResult> callback) {
        String api = "/LmAgentPoint-portlet.userpointtotal/get-user-points-report";
        JSONObject params = ParamsMaker.newInstance().add("reportDateString", reportDateString).add("userId", LineMoneyContext.getLoggedUser().getUserId()).get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                if (null != callback) {
                    try {
                        callback.onSuccess(what, DPGsonUtil.fromJson(result.get(0).toString(), UserPointsReportResult.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (null != callback) {
                    callback.onError(what, 0, e.getLocalizedMessage(), e);
                }
            }
        });
    }

    public static class UserPointsReportResult {
        private Status status;

        private Value value;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setValue(Value value) {
            this.value = value;
        }

        public Value getValue() {
            return this.value;
        }

        public class Status {
            private String message;

            private String code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(String code) {
                this.code = code;
            }

            public String getCode() {
                return this.code;
            }

        }

        public class Value {
            private String report_date;

            private String year_to_date_total_points;

            private String this_month_total_points;

            private String userId;

            private String year_to_date_total_cashed_amount;

            private String year_to_date_transferrable_amount;

            private String year_to_date_cashable_points;

            private String year_to_date_transferred_amount;

            public void setReport_date(String report_date) {
                this.report_date = report_date;
            }

            public String getReport_date() {
                return this.report_date;
            }

            public void setYear_to_date_total_points(String year_to_date_total_points) {
                this.year_to_date_total_points = year_to_date_total_points;
            }

            public String getYear_to_date_total_points() {
                return this.year_to_date_total_points;
            }

            public void setThis_month_total_points(String this_month_total_points) {
                this.this_month_total_points = this_month_total_points;
            }

            public String getThis_month_total_points() {
                return this.this_month_total_points;
            }

            public void setUserId(String userId) {
                this.userId = userId;
            }

            public String getUserId() {
                return this.userId;
            }

            public void setYear_to_date_total_cashed_amount(String year_to_date_total_cashed_amount) {
                this.year_to_date_total_cashed_amount = year_to_date_total_cashed_amount;
            }

            public String getYear_to_date_total_cashed_amount() {
                return this.year_to_date_total_cashed_amount;
            }

            public void setYear_to_date_transferrable_amount(String year_to_date_transferrable_amount) {
                this.year_to_date_transferrable_amount = year_to_date_transferrable_amount;
            }

            public String getYear_to_date_transferrable_amount() {
                return this.year_to_date_transferrable_amount;
            }

            public void setYear_to_date_cashable_points(String year_to_date_cashable_points) {
                this.year_to_date_cashable_points = year_to_date_cashable_points;
            }

            public String getYear_to_date_cashable_points() {
                return this.year_to_date_cashable_points;
            }

            public void setYear_to_date_transferred_amount(String year_to_date_transferred_amount) {
                this.year_to_date_transferred_amount = year_to_date_transferred_amount;
            }

            public String getYear_to_date_transferred_amount() {
                return this.year_to_date_transferred_amount;
            }

        }
    }

    public void transferCash(String transferAmount, String toAccountId, final Callback<CashTransferResult> callback) {
        String api = "/LmAgentPoint-portlet.userpointtotal/user-cash-transfer-request";
        JSONObject params = ParamsMaker.newInstance()
                .add("transferAmount", transferAmount)
                .add("toAccountId", toAccountId)
                .add("userId", LineMoneyContext.getLoggedUser().getUserId())
                .get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                if (null != callback) {
                    try {
                        callback.onSuccess(what, DPGsonUtil.fromJson(result.get(0).toString(), CashTransferResult.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (null != callback) {
                    callback.onError(what, 0, e.getLocalizedMessage(), e);
                }
            }
        });
    }

    public static class CashTransferResult {
        private Status status;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public class Status {
            private String message;

            private String code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(String code) {
                this.code = code;
            }

            public String getCode() {
                return this.code;
            }

        }
    }
}
